package cn.ming.wfchat.app.me.activity;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.me.activity.adapter.BuddyListAdapter;
import cn.ming.wfchat.app.moment.third.beans.BuddyBean;
import cn.ming.wfchat.kit.WfcBaseActivity;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author luoming
 * created at 2020/8/11 3:04 PM
 * 好友列表
 */
public class BuddyListActivity extends WfcBaseActivity {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    BuddyListAdapter buddyListAdapter;
    int page = 1;
    private List<BuddyBean.NetWorkListBean> userBeans;
    private boolean isshuai = true;
    private String level;

    @Override

    protected int contentLayout() {
        return R.layout.my_flowing_water_activity;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        level = getIntent().getStringExtra("level");
        setTitle(level+"级好友");
        initView();
        initDate();
    }

    void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        userBeans = new ArrayList<>();
        buddyListAdapter = new BuddyListAdapter(userBeans);
        recyclerView.setAdapter(buddyListAdapter);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setListener(new SpringView.OnFreshListener() {
            public void onRefresh() {
                page = 1;
                initDate();
            }

            @Override
            public void onLoadmore() {
                if (isshuai) {
                    page++;
                    initDate();
                } else {
                    springView.onFinishFreshAndLoad();
                }

            }
        });

    }

    void initDate() {
        AppService.Instance().findNetWork(page, level, new AppService.BuddyNetWorkCallback() {
            @Override
            public void onUiSuccess(BuddyBean assetsBean) {
                springView.onFinishFreshAndLoad();
                if (assetsBean.getNetWorkList() != null && assetsBean.getNetWorkList().size() > 0) {
                    if (page == 1) {
                        userBeans = assetsBean.getNetWorkList();
                    } else {
                        userBeans.addAll(assetsBean.getNetWorkList());
                    }
                    if (assetsBean.getNetWorkList().size() > 9) {
                        isshuai = true;
                    } else {
                        isshuai = false;
                    }
                } else {
                    userBeans.clear();
                    isshuai = false;
                }
                buddyListAdapter.setNewData(userBeans);
            }

            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);
            }
        });
    }

}
